package com.wechat.shopping.mall.category.controller;

import com.wechat.shopping.mall.category.service.CategoryService;
import com.wechat.shopping.mall.util.ResponseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/categories")
@Api(value = "分类目录操作")
public class CategoryController {
    private static final Logger LOG = LoggerFactory.getLogger(CategoryController.class);

    @Autowired
    private CategoryService categoryService;

    @RequestMapping(value = "/category/info",method = RequestMethod.POST)
    @ApiOperation(value = "商品分类创建",notes = "商品分类创建",httpMethod = "POST",response = ResponseMessage.class)
    public ResponseMessage saveCategory(@RequestParam("categoryName") String categoryName,
                                           @RequestParam("type") Byte type,
                                           @RequestParam("parentId") Integer parentId) {

    return categoryService.saveCategory(categoryName,type,parentId);
    }

    @RequestMapping(value = "/category/{id}/info",method = RequestMethod.PUT)
    @ApiOperation(value = "商品分类修改",notes = "编辑商品分类",httpMethod = "PUT",response = ResponseMessage.class)
    public ResponseMessage editCategory(@PathVariable("id") Integer id,
                                          @RequestParam("categoryName") String categoryName) {
        return 1 == categoryService.editCategory(id,categoryName)? ResponseMessage.ok("编辑商品分类成功"):ResponseMessage.error("编辑商品分类失败");
    }

    @RequestMapping(value = "/category/{id}/info",method = RequestMethod.DELETE)
    @ApiOperation(value = "分类删除",notes = "分类删除",httpMethod = "DELETE",response = ResponseMessage.class)
    public ResponseMessage deleteCategory(@PathVariable("id") Integer id) {
        return 1 == categoryService.deleteCategory(id)? ResponseMessage.ok("分类删除成功"):ResponseMessage.error("分类删除失败");
    }

    @RequestMapping(value = "/categories/{categoryId}/info",method = RequestMethod.GET)
    @ApiOperation(value = "获取顶级菜单",notes = "获取顶级菜单",httpMethod = "GET",response = ResponseMessage.class)
    public ResponseMessage getCategories(@PathVariable("categoryId") Integer categoryId,
                                         @RequestParam("type") Byte type) {
        return categoryService.getCategories(categoryId,type);
    }

    @RequestMapping(value = "/good/{goodId}/info",method = RequestMethod.GET)
    @ApiOperation(value = "获取商品信息",notes = "获取商品信息",httpMethod = "GET",response = ResponseMessage.class)
    public ResponseMessage getGoodById(@PathVariable("goodId") Integer goodId) {
        return categoryService.getGoodById(goodId);
}
}
